package com.bjsxt.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bjsxt.domain.User;
import com.bjsxt.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bjsxt.domain.UserBank;
import com.bjsxt.mapper.UserBankMapper;
import com.bjsxt.service.UserBankService;
@Service
public class UserBankServiceImpl extends ServiceImpl<UserBankMapper, UserBank> implements UserBankService{

    @Autowired
    private UserService userService;


    @Override
    public Page<UserBank> findByPage(Page<UserBank> page, Long usrId) {
        return page(page,new LambdaQueryWrapper<UserBank>().eq(usrId!=null,UserBank::getUserId,usrId));
    }

    @Override
    public UserBank getCurrentuserBank(Long userId) {
       UserBank userBank =  getOne(new LambdaQueryWrapper<UserBank>().eq(UserBank::getUserId,userId).eq(UserBank::getStatus,1));
        return userBank;
    }

    @Override
    public boolean bind(Long userId, UserBank userBank) {
        //支付密码判断
        String payPassword = userBank.getPayPassword();
        User user = userService.getById(userId);

        BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
        if(!bCryptPasswordEncoder.matches(payPassword,user.getPaypassword()))
        {
            throw  new IllegalArgumentException("支付密码错误");
        }


        Long id = userBank.getId();
        if(id!=null){
            UserBank userBank1 = getById(id);
            if(userBank1==null){
                throw  new IllegalArgumentException("用户银行卡id输入错误");
            }
          return   updateById(userBank);
        }

        //若用户银行卡id为空 新建userbank
        userBank.setUserId(userId);

        return save(userBank);
    }




















}
